package com.imy.accessibility.node;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imy.accessibility.R;
import com.imy.accessibility.bean.CustomNode;
import com.imy.accessibility.view.BaseNodeViewGroup;
import com.imy.common.event.NodeEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class NodeView extends BaseNodeViewGroup {
  private TextView nodeTv;
  private RelativeLayout childNodeRl;

  public NodeView(Context context) {
    super(context);
  }

  public NodeView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public NodeView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  private float downX;
  private float downY;

  @Override
  protected void init(View inflate) {
    nodeTv = inflate.findViewById(R.id.node_view_tv);
    childNodeRl = inflate.findViewById(R.id.child_node_rl);
    setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mOnChildNodeLongClicked != null) {
          mOnChildNodeLongClicked.onClicked(mNodeInfo, 0, 0);
        }
      }
    });
    //setOnTouchListener(new OnTouchListener() {
    //  @Override
    //  public boolean onTouch(View v, MotionEvent event) {
    //    switch (event.getAction()){
    //      case MotionEvent.ACTION_DOWN:
    //        downX =event.getRawX();
    //        downY =event.getRawY();
    //        break;
    //      case MotionEvent.ACTION_UP:
    //        float rawX = event.getRawX();
    //        float rawY = event.getRawY();
    //        if (Math.abs(rawX-downY)<10 && Math.abs(rawY-downY)<10){
    //          if (mOnChildNodeLongClicked!=null){
    //            mOnChildNodeLongClicked.onClicked(mNodeInfo,rawX,rawY);
    //          }
    //        }
    //        break;
    //    }
    //    return true;
    //  }
    //});

  }

  private CustomNode mNodeInfo;

  public void setNode(CustomNode node) {
    //第一层节点没有展示的意义
    this.mNodeInfo = node;

    CharSequence className = node.mClassName;
    String viewIdResourceName = node.mViewIdResourceName;
    if (viewIdResourceName == null) {
      nodeTv.setText(className);
    } else {
      nodeTv.setText(className + " IdRes:" + viewIdResourceName);
    }
    nodeTv.setVisibility(VISIBLE);
  }

  @Override
  protected int getLayoutID() {
    return R.layout.node_view;
  }

  public void setDeep(int currentDeep) {
    this.currentDeep = currentDeep;
  }

  private int currentDeep;

  public void setDeepBackground() {
    childNodeRl.setBackground(getDeepBackground());
  }

  private Drawable getDeepBackground() {

    return getResources().getDrawable(R.drawable.node_view_red_bg);
  }

  public interface OnChildNodeClicked {

    void onClicked(CustomNode nodeInfo, float x, float y);
  }

  public void setOnChildNodeClicked(OnChildNodeClicked longClicked) {
    this.mOnChildNodeLongClicked = longClicked;
  }

  public void setPosition(int i) {
    setDeepBackground();
    //设置背景颜色
    int color = getColor(i);
    nodeTv.setBackgroundColor(color);
  }

  private int getColor(int i) {
    int i1 = i % 5;

    if (i1 == 0) {
      return getResources().getColor(R.color.bg_0);
    } else if (i1 == 1) {
      return getResources().getColor(R.color.bg_1);
    } else if (i1 == 2) {
      return getResources().getColor(R.color.bg_2);
    } else if (i1 == 3) {
      return getResources().getColor(R.color.bg_3);
    } else if (i1 == 4) {
      return getResources().getColor(R.color.bg_4);
    }
    return getResources().getColor(R.color.bg_0);
  }

  private void setEmptyBackground() {
    childNodeRl.setBackground(null);
  }

  @Override
  public void onDestroy() {

    super.onDestroy();
  }

  private OnChildNodeClicked mOnChildNodeLongClicked;
}
